/**
 * 
 */
var VehicleForm = {
	init: function(){
		VehicleForm.initCombo();
		VehicleForm.bind();
	},
	bind : function(){
		  $('#vehicleNo').keyup(function(){
			 
			  var vehicleNumber = $('#vehicleNo').val();
			  var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
			  if (vehicleNumber.length == 7){
				  var result = express.test(vehicleNumber);
				  return result;
				  if(!result){
					  global.ajaxLoading.pop("请输入正确车牌号码",false,2000);
				  }
			  }
			  
		  });
		  
	},
	getParams : function(){
		var vehicleId = $(vehicle)[0].id;
		var vehicleNo = $('#vehicleNo').val();
		var vehicleColor =$('#vehicleColor').combobox('getValue');
		var tradeId = $('#trade').combobox('getValue');
		var OrganizationID = $('#OrganizationID').combobox('getValue');
		var EnterpriseID = $('input[name="enterpriseId"]').val(); 
		var RoadTransportNum = $('#RoadTransportNum').val();
		var inputStartDate = $('#inputStartDate').val();
		var inputEndDate =$('#inputEndDate').val();
		var VehTerminalID = $('#VehTerminalID').combobox('getValue');
		var vehicleType = $('#vehicleType').combobox('getValue');
		var vehicleLevel = $('#vehicleLevel').combobox('getValue');
		var approvedSeat = $('#approvedSeat').val();
		var approvedTonnage = $('#approvedTonnage').val();
		var Origin = $('#Origin').val();
		var Destination = $('#Destination').val();
		var province = $('#province').val();
		var city = $('#city').val();
		var action = $('#action').val();
		var bussinessScope = $('#BusinessScopeCode').combobox('getValue');
		var linkMan = $('#linkMan').val();
		var telePhone = $('#telePhone').val();
		var sms = $('#sms').combobox('getValue');
		var status = $('#status').val();
		if(linkMan !=null && linkMan == '请输入联系人'){
			linkMan = '';
		}
		if(telePhone !=null && telePhone == '请输入联系手机号码'){
			telePhone = '';
		}
		if(inputStartDate ==null || inputStartDate ==''){
			inputStartDate =null;
		}
		if(inputEndDate ==null || inputEndDate ==''){
			inputEndDate =null;
		}
		if(approvedSeat ==null || approvedSeat ==''){
			approvedSeat =0;
		}
		if(approvedTonnage ==null || approvedTonnage ==''){
			approvedTonnage =0;
		}
		if(vehicleNo == null || vehicleNo == "请输入车牌号码"){
			global.ajaxLoading.pop("车牌号不能为空",false,3000);
			return ;
		}else{			 
			  var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
			  if (vehicleNo.length == 7){
				  var result = express.test(vehicleNo);				 
				  if(!result){
					  global.ajaxLoading.pop("请输入正确车牌号码",false,2000);
					  return ;
				  }
			  }else{
				  global.ajaxLoading.pop("请输入七位车牌号码",false,2000);
				  return ;
			  }
		}
		if(tradeId == "0" || tradeId =='' ){
			global.ajaxLoading.pop("请选择所属行业",false,3000);
			return ;
		}
		
		if(EnterpriseID ==undefined || EnterpriseID == "0" || EnterpriseID =='' ){
			global.ajaxLoading.pop("请选择所属业户",false,3000);
			return ;
		}
		if( province==null|| city==null ||RoadTransportNum==null || RoadTransportNum==''){
			global.ajaxLoading.pop("请输入道路运输证号",false,3000);
			return ;
		}
		var params = {
				vehicleId : vehicleId ,
				action : action,
				vehicleNo : $.trim(vehicleNo),
				colorId : vehicleColor,
				tradeId : tradeId , 
				organzationId :OrganizationID,
				enterpriseId : EnterpriseID , 
				transportPermitsion :$.trim(RoadTransportNum),
				startTime : inputStartDate ,
				endTime : inputEndDate ,
				teminalId : VehTerminalID ,
				kindId : vehicleType,
				vehicleLevel : vehicleLevel,
				approvedSeat : approvedSeat ,
				approvedTonnage : approvedTonnage ,
				orgin : Origin ,
				destination : Destination,
				city : city,
				province : province,
				bussinessScope : bussinessScope,
				linkMan : linkMan,
				telePhone : telePhone,
				sms : sms,
				status:status
		};
		return params;
	},
	/**
	 * 保存车辆
	 */
	saveVehicle: function() {
		var data = VehicleForm.getParams();
		if(data){
			$.ajax({
				url : appCtx+"base/vehicleManager/saveVehicle.html"  ,
				type: 'post',
				contentType : "application/json; charset=utf-8",
				data: JSON.stringify(data),
			    cache: false,
			    dataType : "JSON",
			    success: function (data) {
			    	if (data.code == "0") {
						global.ajaxLoading.pop(data.msg,false,2000);
						closeDataDialog();
						var funStr = "window.frames['" + mFrameName + "']."+ mDialogCallback;
						eval("(" + funStr + ")");
					}else if (data.code == "1") {
						showVelidateModal(data.msg);
						return;
					}  else {
						showVelidateModal(data.msg);
						return;
					}
					
				}
			    
			});
		}
	},
	
	initCombo: function(){
		if(action == 'insert'){
			$('#vehicleNo').val('');
			$('#approvedSeat').val('');
			$('#approvedTonnage').val('');
			$('#Origin').val('');
			$('#Destination').val('');
			$('#RoadTransportNum').val('');
		}else{
			$('#status').val($(vehicle)[0].isdeleted);
			$('#status').select2();
			$('#vehicleNo').val($(vehicle)[0].vehicleNo);
			$('#approvedSeat').val($(vehicle)[0].approvedSeat);
			$('#approvedTonnage').val($(vehicle)[0].approvedTonnage);
			$('#Origin').val($(vehicle)[0].origin);
			$('#Destination').val($(vehicle)[0].destination);
			$('#RoadTransportNum').val($(vehicle)[0].transportPermitsion);
			$('#inputStartDate').val($(vehicle)[0].startTime);
			$('#inputEndDate').val($(vehicle)[0].endTime);
			$('#linkMan').val($(vehicle)[0].linkMan);
			$('#telePhone').val($(vehicle)[0].telePhone);
			var arr=[10,11,12,13,14,15,16];
	        var index =  arr.indexOf($(vehicle)[0].kindId);
	    	if(index !=-1 ){
	    		$('.keche').show();
	    		$('.huoche').hide();
	    	}else{
	    		$('.keche').hide();
	    		$('.huoche').show();
	    	}
			
		}
		$('#vehicleLevel').combobox({
			valueField : 'id',
			textField : 'text',
			editable : false,
			data :[
			       { id: 1, text: '普通级'},
			       { id: 2, text: '中级'},
			       { id: 3, text: '高一级'},
			       { id: 4, text: '高二级'},
			       { id: 5, text: '高三级'}
			      ],
			
		     onLoadSuccess :function(data){
		    	 if(action == 'insert'){
		    		 $('#vehicleLevel').combobox('select',1); 
		    	 }else{
		    		 var id = $(vehicle)[0].vehicleLevel;
		    		 $('#vehicleLevel').combobox('select',id); 
		    	 }
		     }
		});
		$('#sms').combobox({
			valueField : 'id',
			textField : 'text',
			editable : false,
			data :[
			       { id: 0, text: '停用'},
			       { id: 1, text: '启用'}			    
			      ],
			
		     onLoadSuccess :function(data){
		    	 if(action == 'insert'){
		    		 $('#sms').combobox('select',0); 
		    	 }else{
		    		 var id = $(vehicle)[0].enable_sms;
		    		 $('#sms').combobox('select',id); 
		    	 }
		     }
		});
		var BusinessScopeCode = $('#BusinessScopeCode').combotree({
			url : appCtx + "system/common/queryBusinessScopeTree.html",
			valueField: 'id',
	        textField: 'text',
	        editable : false,
	        loadFilter: function(data){
	        	data.unshift({ id: 0, text: '请选择经营范围类型' });
	            return data;
	    	},
	    	onLoadSuccess : function(data){
	    		if(action=='insert'){
	    			$('#BusinessScopeCode').combotree('tree').tree("collapseAll");
	    			$('#BusinessScopeCode').combotree('select',0);
    			}else{
    				var id = $(vehicle)[0].bussinessScopeId;
    				$('#BusinessScopeCode').combotree('tree').tree("collapseAll");
    				$("#BusinessScopeCode").combotree('setValue',id);
    			}
    		}
		});
		BusinessScopeCode.combogrid('textbox').keyup(function(event) {
			$('#BusinessScopeCode').combotree('tree').tree("search", $(this).val());
		});
		$('#trade').combobox({
			url:appCtx + "/system/common/queryAllTrade.html",
			valueField: 'id',
            textField: 'text',
            editable : false,
            loadFilter: function(data){
            	 data.unshift({ id: 0, text: '请选择所属行业'});
                 return data;
    		},
    		onLoadSuccess : function(data){
    			if(action == 'insert'){
    				$('#trade').combobox('select',0);
    			}else{
    				var id = $(vehicle)[0].tradeId;
    				$('#trade').combobox('select',id);
    			}
    		}
		});
		
		$('#EnterpriseID').combobox({
			url:appCtx + "/system/common/queryEnterprise.html?pageSize=9999",
			valueField: 'id',
            textField: 'text',
            editable : true,
            loadFilter: function(data){
            	 data.unshift({ id: 0, text: '请选择所属业户' });
                 return data;
    		},
    		onLoadSuccess : function(data){
    			if(action == 'insert'){
    				$('#EnterpriseID').combobox('select',0);
    			}else{
    				var id = $(vehicle)[0].enterpriseId;
    				$('#EnterpriseID').combobox('select',id);
    			}
    		}
    		
		});
		
		var organization = $('#OrganizationID').combotree({

			url:appCtx + "/system/common/queryOrganzationTree.html",
			valueField: 'id',
            textField: 'text',
            method: 'post',
            editable : false,
            loadFilter: function(data){
            	 data.unshift({ id: 0, text: '请选择机构'});
                 return data;
    		},
    		onLoadSuccess: function (data) {
    			if(action=='insert'){
	    			$('#OrganizationID').combotree('tree').tree("collapseAll");
	    			$('#OrganizationID').combotree('select',0);
    			}else{
    				var id = $(vehicle)[0].organzationId;
    				$('#OrganizationID').combotree('tree').tree("collapseAll");
    				$("#OrganizationID").combotree('setValue',id);
    			}
         	}
			
		});
		organization.combogrid('textbox').keyup(function(event) {
			$('#OrganizationID').combotree('tree').tree("search", $(this).val());
		});
		
		var vehicleType = $('#vehicleType').combotree({
			url:appCtx + "/system/common/queryVehicleTypeTree.html",
			valueField: 'id',
            textField: 'text',
            editable : false,
            loadFilter: function(data){
            	  data.unshift({ id: 0, text: '请选择车辆类型' });
                  return data;
    		},
    		onLoadSuccess: function (data) {
    			if(action == 'insert'){
	    			$('#vehicleType').combotree('tree').tree("collapseAll");
	    			$('#vehicleType').combotree('select',0);
    			}else{
    				var id = $(vehicle)[0].kindId;
    				$('#vehicleType').combotree('tree').tree("collapseAll");
    				$("#vehicleType").combotree('setValue',id);
    			}
         	},
         	onSelect : function(data){
	         	var arr=["10","11","12","13","14","15","16"];
	         	var index =  arr.indexOf(data.id);
	    		if(index !=-1 ){
	    			$('.keche').show();
	    			$('.huoche').hide();
	    		}else{
	    			$('.keche').hide();
	    			$('.huoche').show();
	    		}
         	}
		});
		vehicleType.combogrid('textbox').keyup(function(event) {
			$('#vehicleType').combotree('tree').tree("search", $(this).val());
		});
		$('#VehTerminalID').combobox({
			url:appCtx + "/system/common/queryTeminalType.html",
			valueField: 'ID',
            textField: 'text',
            editable : false,
            loadFilter: function(data){
            	data.unshift({ ID: 0, text: '请选择终端类型' });
                return data;
            },
            onLoadSuccess : function(data){
            	if(action=='insert'){
            		$('#VehTerminalID').combobox('select',0);
            	}else{
            		var id=$(vehicle)[0].teminalId;
            		$('#VehTerminalID').combobox('select',id);
            	}
            }
		});
		
		//协议规定车辆颜色
		$('#vehicleColor').combobox({
			valueField : 'id',
			textField : 'text',
			editable : false,
			data :[
			       { id: 2, text: '黄色'},
			       { id: 4, text: '白色'},
			       { id: 1, text: '蓝色'},
			       { id: 3, text: '黑色'},
			       { id: 9, text: '其他'}
			      ],
			
		     onLoadSuccess :function(data){
		    	 if(action == 'insert'){
		    		 $('#vehicleColor').combobox('select',1); 
		    	 }else{
		    		 var id = $(vehicle)[0].colorId;
		    		 $('#vehicleColor').combobox('select',id); 
		    	 }
		     }
		});
		
	},
	
	
};

VehicleForm.init();